var MD5 = require('/MD5.js');  
/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function get(url, success, fail) {

  console.log("------start---_get----");
  console.log(url);
  wx.request({
    url: url,
    header: {
      'Content-Type': 'application/json'
    },
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      console.log(res)
      fail(res);
    }
  });
  console.log("----end-----_get----");
}

/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function post(url, Data, success, fail) {
  //{"RequestMetaInfo":{"SiteId": 1,"TimeSpan": 1, "Sign": 1},Data:{}}
  var data = new Object();

  var RequestMetaInfo = new Object();
  RequestMetaInfo.SiteId = 10;
  RequestMetaInfo.TimeSpan = new Date().getTime();
  RequestMetaInfo.Sign = MD5.hexMD5(RequestMetaInfo.SiteId + 'Y()&*((*^*416!#$%sdgmv' + RequestMetaInfo.TimeSpan);
  
  data.RequestMetaInfo = RequestMetaInfo;

  data.Data = Data;

  var jsonData = JSON.stringify(data)
  console.log(jsonData);

  console.log("----_post--start-------");
  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: { data: jsonData },
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });
  console.log("----end-----_get----");
}


/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function post1(url, data, success, fail) {
  console.log("----_post--start-------");
  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: data,
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });
  console.log("----end-----_get----");
}
/**
* url 请求地址
* success 成功的回调
* fail 失败的回调
*/
function post_json(url, data, success, fail) {
  console.log("----_post--start-------");
  wx.request({
    url: url,
    // header: {
    //     'content-type': 'application/json',
    // },
    method: 'POST',
    data: data,
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });

  console.log("----end----_post-----");
}
module.exports = {
  get: get,
  post: post,
  post1: post1,
  post_json: post_json
}
